package com.xcx.robot.spring.aop;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xcx.robot.mirai.config.UserConfig;
import com.xcx.robot.mirai.key.KeyWord;
import com.xcx.robot.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import net.mamoe.mirai.contact.Contact;
import net.mamoe.mirai.contact.Group;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.List;

/**
 * @Author: 邢晨旭
 * @Date: 2021/6/9 09:32
 * @Description: around > before > around > after > afterReturning
 */

@Slf4j
@Aspect
@Component
public class InterceptorAspect {


    @Pointcut("execution(public * com.xcx.robot.mirai.factory.BaseFacade.execute(..))")
    public void messagePointcut() {
    }

    @Before("messagePointcut()")
    public void beginTransaction(JoinPoint joinPoint) throws NoSuchFieldException, IllegalAccessException {


    }

    @After("messagePointcut()")
    public void commit() {
//        System.out.println("after commit");
    }

    @AfterReturning(value = "messagePointcut()", returning = "returnObject")
    public void afterReturning(JoinPoint joinPoint, Object returnObject) {
//        System.out.println("afterReturning");
    }

    @AfterThrowing("messagePointcut()")
    public void afterThrowing() {
//        System.out.println("afterThrowing afterThrowing  rollback");
    }

    @Around("messagePointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
//        System.out.println("around");
        if (keyWordVerify(joinPoint)) {
            log.info("执行方法");
            return joinPoint.proceed();
        } else {
            return null;
//            throw new NullPointerException();
        }
    }

//    @Resource
//    RedisUtil<List> redisUtil;

    private boolean keyWordVerify(JoinPoint joinPoint) throws Exception {
        Object target = joinPoint.getTarget();
        Class c = target.getClass();
        //通过类对象获取单个属性
        Field f = c.getField("keyWord");//f为当前指定类属性的file属性
        //获取属性值
        Object o = f.get(target);
        KeyWord keyWord = new ObjectMapper().convertValue(o, KeyWord.class);
        log.info("属性-{}", keyWord);
        Contact group = (Contact) joinPoint.getArgs()[1];
        if(group!=null){
            if (UserConfig.ImageGroups.contains(group.getId()) && !(keyWord.equals(KeyWord.IMAGE)||keyWord.equals(KeyWord.MAGNETIC))) {
                return false;
            }
        }
//        if()
//        List<KeyWord> keyWords = redisUtil.getResult("TeamAuthority:" + group.getId(), List.class);
//        if (keyWords!=null && !keyWords.contains(keyWord)) {
//            return false;
//        }
        String content = joinPoint.getArgs()[2].toString();
        return (keyWord.code == 3 || (keyWord.code == 2 && content.indexOf(keyWord.keyWord + " ") == 0) || (keyWord.code == 1 && keyWord.keyWord.equals(content)));
    }
}

